Ext.define('AM.controller.user.UsersController', {
	extend : 'Ext.app.Controller',
	alias : 'controller.users',
	views : [ 'user.List', 'user.Edit', 'login.Login', 'order.OrderItems' ],
	stores : [ 'Users' ],
	models : [ 'User' ],
	init : function(view) {
		this.control({
			'viewport>userlist' : {
				itemdblclick : this.editUser
			},
			'useredit button[action=save]' : {
				click : this.updateUser
			},
			'viewport>userlist button[action=add]' : {
				click : this.onaddOperation
			},
			'viewport>userlist button[action=delete]' : {
				click : this.ondeleteOperation
			}
		});
	},
	onaddOperation : function(grid, record) {
		var view = Ext.widget('userlogin');
/*
		alert(view == grid);
		var store = view.getStore();
		var i = store.last().get('id');
		var data = {
			id : ++i,
			name : 'ak' + i,
			email : 'ak' + i + '@sanmina.com'
		}
		store.add(data);*/

	},
	ondeleteOperation : function() {
		var view = Ext.widget('orderitems');
		/*
		 * var grid = Ext.widget('userlist'), store = grid.getStore(), selected =
		 * grid.getSelectionModel().getSelection(); alert(selected);
		 * store.remove(selected);
		 */
	},
	editUser : function(grid, record) {

		var view = Ext.widget('useredit');
		view.down('form').loadRecord(record);

	},
	updateUser : function(button) {
		var win = button.up('window'), form = win.down('form'), record = form
				.getRecord(), values = form.getValues();
		alert(record)
		record.set(values);
		win.close();
		this.getUsersStore().sync();
	}
});